<template>
  <div class="maskCenter">
    <div
      id="box"
      :class="visible ? 'systemDialog' : 'systemDialog-hide'"
      style="height: 558px; width: 436px"
    >
      <div class="dialog-header" v-movew>
        <span>{{ title }}</span>
        <el-tooltip
          class="item"
          effect="dark"
          content="关闭"
          placement="top-start"
        >
          <span
            class="icon iconfont iconbiaoqianlan-guanbi close"
            @click="hide"
          ></span>
        </el-tooltip>
      </div>
      <div class="dialog-body">
        <el-tabs v-model="activeName">
          <el-tab-pane label="基本属性" name="first">
            <el-form
              ref="firstform"
              label-width="75px"
              :label-position="'left'"
            >
              <el-form-item label="计划名称:">
                <el-col :span="24">
                  <el-input
                    class="systemInput"
                    v-model="modelinfo.name"
                  ></el-input>
                </el-col>
              </el-form-item>

              <el-form-item label="计划版本:">
                <el-col :span="17">
                  <el-input
                    class="systemInput"
                    v-model="modelinfo.version"
                  ></el-input>
                </el-col>
                <el-col :span="7" style="text-align: right">
                  <el-checkbox systemCheckbox v-model="modelinfo.active"
                    >活动版本</el-checkbox
                  >
                </el-col>
              </el-form-item>

              <el-form-item label="描述信息:">
                <el-col :span="24">
                  <el-input
                    class="systemInput"
                    v-model="modelinfo.note"
                  ></el-input>
                </el-col>
              </el-form-item>
              <el-form-item label="消息类型:">
                <el-col :span="5" style="text-align: left">
                  <el-checkbox systemCheckbox v-model="modelinfo.sms"
                    >短信</el-checkbox
                  >
                </el-col>
                <el-col :span="7" style="text-align: left">
                  <el-checkbox systemCheckbox v-model="modelinfo.jpush"
                    >极光推送</el-checkbox
                  >
                </el-col>
              </el-form-item>

              <el-form-item class="noLabel">
                <el-col :span="24">
                  <el-tabs v-model="scriptActiveName">
                    <el-tab-pane label="启动脚本" name="first">
                      <el-input
                        class="systemInput"
                        type="textarea"
                        :autosize="{ minRows: 6, maxRows: 6 }"
                        v-model="modelinfo.startScript"
                        :resize="'none'"
                      ></el-input>
                    </el-tab-pane>
                    <el-tab-pane label="结束脚本" name="second">
                      <el-input
                        class="systemInput"
                        type="textarea"
                        :autosize="{ minRows: 6, maxRows: 6 }"
                        v-model="modelinfo.endScript"
                        :resize="'none'"
                      ></el-input>
                    </el-tab-pane>
                    <el-tab-pane label="作废脚本" name="third">
                      <el-input
                        class="systemInput"
                        type="textarea"
                        :autosize="{ minRows: 6, maxRows: 6 }"
                        v-model="modelinfo.outScript"
                        :resize="'none'"
                      ></el-input>
                    </el-tab-pane>
                  </el-tabs>
                </el-col>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="自定义变量" name="second">
            <div class="borderdiv">
              <el-table
                ref="varTable"
                :data="modelinfo.vvar"
                @row-click="rowselected"
                highlight-current-row
                :stripe="true"
                :height="'269px'"
                :max-height="'317px'"
                style="width: 100%"
              >
                <el-table-column
                  prop="name"
                  label="变量名"
                  width="180"
                ></el-table-column>
                <el-table-column prop="note" label="描述"></el-table-column>
                <el-table-column prop="value" label="缺省值"></el-table-column>
              </el-table>
            </div>

            <el-row type="flex">
              <div class="btnAdd margin-ten" @click="addVar">
                <span
                  class="iconfont iconzengjia"
                  style="
                    margin-left: 16px;
                    height: 28px;
                    line-height: 26px;
                    width: 13px;
                    margin-right: 6px;
                    color: var(--icon-new-color);
                  "
                ></span>
                <span>新增</span>
              </div>
              <div class="btnEdit margin-ten" @click="editVar">
                <span
                  class="iconfont iconbiaoge-bianji"
                  style="
                    margin-left: 16px;
                    height: 28px;
                    line-height: 26px;
                    width: 13px;
                    margin-right: 6px;
                    color: var(--icon-edit-color);
                  "
                ></span>
                <span>编辑</span>
              </div>
              <div class="btnDelete margin-ten" @click="delVar">
                <span
                  class="iconfont iconbiaoge-shanchu"
                  style="
                    margin-left: 16px;
                    height: 28px;
                    line-height: 26px;
                    width: 13px;
                    margin-right: 6px;
                    color: var(--icon-del-color);
                  "
                ></span>
                <span>删除</span>
              </div>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="系统变量" name="third">
            <div class="borderdiv">
              <el-table
                :data="modelinfo.sysVars"
                :height="'313px'"
                :max-height="'361px'"
                style="width: 100%"
              >
                <el-table-column
                  prop="name"
                  label="变量名"
                  width="180"
                ></el-table-column>
                <el-table-column prop="note" label="描述"></el-table-column>
                <el-table-column prop="value" label="缺省值"></el-table-column>
              </el-table>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
      <div class="dialog-footer right">
        <span class="systemButtonConfirm" @click="okclick">确认</span>
        <span class="systemButtonConfirm" @click="hide">取消</span>
      </div>
      <winBusinessesVar
        v-if="showWinVar"
        @close="closeWinVar"
        @ok="saveVar"
        :activeVar="selectedVar"
        :operatetype="operatetype"
      ></winBusinessesVar>
    </div>
  </div>
</template>

<script>
import movew from "@/directives/winmove";
import winBusinessesVar from "@/system/components/winBusinessesVar.vue";
import { deepCopy } from "@/utils/public";
export default {
  directives: { movew },
  name: "winBussinessModel",
  props: {
    activeModel: { default: null },
  },
  components: { winBusinessesVar },
  data() {
    return {
      visible: true,
      tableAttr: null,
      title: "业务模型属性",
      modelinfo: {}, //name:   value: "20"
      activeName: "first",
      scriptActiveName: "first",
      tvar: {
        id: null,
        name: null,
        note: null,
        value: null,
      },
      showWinVar: false,
      operatetype: "new",
      selectedVar: null,
      selectedRowIndex: -1,
    };
  },
  mounted() {
    if (this.activeModel) {
      this.modelinfo = deepCopy(this.activeModel);
    } else {
    }
  },
  methods: {
    hide() {
      this.visible = false;
      setTimeout(() => {
        this.$emit("close");
      }, 400);
    },
    okclick() {
      this.visible = false;
      setTimeout(() => {
        this.$emit("ok", this.modelinfo);
      }, 400);
    },

    rowselected(row, column, event) {
      console.log(row);
      this.selectedVar = row;
      this.selectedRowIndex = this.varIndex(row.name);
      //				this.$refs.varTable.setCurrentRow(row);
    },
    closeWinVar() {
      this.showWinVar = false;
    },
    varIndex(name) {
      let tindex = -1;
      if (this.modelinfo.vvar) {
        this.modelinfo.vvar.forEach((item, index) => {
          if (item.name == name) {
            tindex = index;
          }
        });
      }
      return tindex;
    },
    existVarName(name) {
      let tindex = -1;
      if (this.modelinfo.vvar) {
        this.modelinfo.vvar.forEach((item, index) => {
          if (this.selectedRowIndex != index) {
            if (item.name == name) {
              tindex = index;
            }
          }
        });
      }
      return tindex;
    },
    saveVar(val, type) {
      this.showWinVar = false;
      if (type) {
        if (type == "new") {
          if (this.varIndex(val.name) == -1) {
            this.modelinfo.vvar.push(val);
          } else {
            this.message("变量" + val.name + "已经存在！", 3);
          }
        }
        if (type == "edit") {
          let tindex = this.existVarName(val.name);
          if (tindex > -1) {
            this.message("变量" + val.name + "已经存在！", 3);
          } else {
            this.modelinfo.vvar.splice(this.selectedRowIndex, 1);
            this.modelinfo.vvar.splice(this.selectedRowIndex, 0, val);
            //							this.modelinfo.vvar.push(val);
            //							this.modelinfo.vvar[this.selectedRowIndex]=val;
            //							for(let key in val){
            //								this.$set(this.modelinfo.vvar[this.selectedRowIndex],key,val[key]);
            //							}
            console.log(this.modelinfo.vvar);
          }
        }
      }
    },
    addVar() {
      this.operatetype = "new";
      this.showWinVar = true;
      this.selectedVar = null;
      this.selectedRowIndex = -1;
    },
    editVar() {
      if (this.selectedVar) {
        this.operatetype = "edit";
        this.showWinVar = true;
      } else {
        this.message("请选中一条数据编辑！", 3);
      }
    },
    delVar() {
      if (this.selectedVar) {
        this.messageBox("确定删除？").then((res) => {
          if (res.action) {
            this.modelinfo.vvar.splice(this.selectedRowIndex, 1);
            this.selectedVar = null;
            this.selectedRowIndex = -1;
          }
        });
      } else {
        this.message("请选中一条数据编辑！", 3);
      }
    },
  },
};
</script>

<style scoped>
.mask {
  height: 100%;
  width: 100%;
  position: fixed;
  top: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 99;
  display: flex;
  align-items: center;
  justify-content: center;
}

.header {
  display: flex;
  justify-content: space-between;
  height: 40px;
  line-height: 40px;
  background: var(--win-head-back-color);
  margin: 4px;
  text-align: left;
}
.header .title {
  width: calc(100% - 25px);
  height: 40px;
  line-height: 40px;
  font-size: 16px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: var(--system-page-topbar-text-color);
  margin-left: 12px;
}
.header .close {
  font-size: 12px;
  width: 12px;
  height: 12px;
  margin-right: 9px;
}

.dialog-body {
  flex-direction: row;
  color: var(--system-page-topbar-text-color);
}
/*表单背景宽高设置*/

.open-content {
  background: var(--win-page-back-color);
  box-shadow: 0px 0px 6px 0px var(--win-page-shadow-color);
  border-radius: 4px;
  overflow: hidden;
  animation: myscale 0.4s;
}
/*表单打开动画*/

@keyframes myscale {
  0% {
    opacity: 0;
    transform: scale(0.6);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}
/*表单关闭样式及动画*/

.open-content-hide {
  background: var(--win-page-back-color);
  box-shadow: 0px 10px 20px 0px var(--win-page-shadow-color),
    0px -10px 20px 0px var(--win-page-shadow-color);
  border-radius: 4px;
  overflow: hidden;
  animation: myscalehide 0.4s;
  transform: scale(0.6);
  opacity: 0;
}

@keyframes myscalehide {
  0% {
    opacity: 1;
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(0.6);
  }
}

@keyframes fadeInLeft {
  0% {
    opacity: 0;
    transform: translateX(-120px);
  }
  100% {
    opacity: 1;
    transform: translateX(0);
  }
}
.margin-ten {
  margin-right: 10px;
}
.btndiv {
  border: 1px solid var(--button-border-color);
  border-radius: 4px;
  margin-left: 5px;
  line-height: 22px;
  height: 32px;
  background: var(--button-back-color);
}

.btndiv:hover {
  background: var(--button-hover-back-color);
  color: var(--button-active-text-color);
  cursor: pointer;
}

.btndiv:active {
  background: var(--button-active-back-color);
  color: var(--button-active-text-color);
  cursor: pointer;
}
/*每行的标签高度设为32px*/
>>> .el-form-item__label {
  line-height: 32px;
}
/*设置el-form-item的高度*/
>>> .el-form-item__content {
  height: 32px;
  line-height: 32px;
}
/*数字输入框的上箭头行高*/
>>> .el-input-number__increase,
>>> .el-input-number__decrease {
  line-height: 16px !important;
}
/*数字输入样式*/
>>> .el-input-number {
  line-height: 32px;
  width: 100%;
}
/*element 输入框样式*/
>>> .el-input__inner {
  line-height: 32px;
  height: 32px;
  width: 100%;
  border: 1px solid var(--input-border-color);
  border-radius: 4px;
  color: var(--input-text-color);
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 400;
}
/*element 下拉框*/

>>> .el-input__inner:focus {
  border: 1px solid var(--input-focus-border-color);
}
/*下拉框箭头*/

>>> .el-select__caret {
  line-height: 32px;
}
/*el-form-item 每行离底部多元*/
>>> .el-form-item {
  margin-bottom: 16px;
}
/*tabs 样式*/
>>> .el-tabs__header {
  margin: 0;
  margin-bottom: 16px;
}
.el-tabs {
  height: 100%;
}
>>> .el-tabs__active-bar {
  width: 80px !important;
}
>>> .el-tabs--top .el-tabs__item.is-top:nth-child(2) {
  padding-left: 0px;
}
>>> .el-tabs__item {
  padding: 0 !important;
  font-weight: bold;
  letter-spacing: 2px;
  height: 26px;
  line-height: 26px;
  color: var(--disable-text-color);
  text-align: center;
  border: none;
  min-width: 80px;
}
/*活动颜色*/
>>> .el-tabs__item.is-active {
  color: var(--active-color);
}
/*tabs 分割线*/
>>> .el-tabs__nav-wrap::after {
  height: 1px;
  background-color: var(--page-hr-line-color);
}
/*tabs 前面没有标签内容要显示,去除margin-left*/
.noLabel >>> .el-form-item__content {
  margin-left: 0 !important;
}
/*element 输入框的 focus hover样式*/
>>> .el-input__inner:focus,
>>> .el-textarea__inner:focus {
  background: var(--input-focus-back-color);
  border: 1px solid var(--input-focus-border-color);
  border-radius: 4px;
}

>>> .el-input__inner:hover,
>>> .el-textarea__inner:hover {
  background: var(--input-hover-back-color);
  border: 1px solid var(--input-focus-border-color);
}
>>> .el-textarea__inner {
  color: var(--input-text-color);
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 400;
}
>>> .el-form-item__label,
>>> .el-checkbox__label {
  color: var(--page-lable-text-color);
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 400;
}

.footer {
  padding-right: 20px;
  height: 60px;
  line-height: 60px;
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  margin-bottom: 20px;
}
.iconbtn {
  width: 79px;
  height: 28px;
  border: 1px solid var(--button-border-color);
  border-radius: 4px;
  line-height: 28px;
  color: var(--text-color);
  margin-right: 10px;
}
.iconbtn:hover {
  background: var(--button-hover-back-color);
  color: var(--button-active-text-color);
  cursor: pointer;
}
.iconbtn:active {
  background: var(--button-active-back-color);
  color: var(--button-active-text-color);
  cursor: pointer;
}
.borderdiv {
  border: 1px solid var(--button-border-color);
  border-radius: 4px;
  margin-bottom: 16px;
}
/*表样式*/
>>> .el-table {
  border-radius: 4px;
  color: var(--table-text-color);
}
>>> .el-table thead {
  color: var(--table-head-text-color);
}
>>> .el-table th,
.el-table tr {
  background: var(--table-head-back-color);
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 400;
}
/*table active 颜色*/
>>> .current-row td {
  color: var(--button-active-text-color) !important;
  background: var(--button-active-back-color) !important;
}
/*table hover 颜色*/
>>> .el-table tbody tr:hover > td {
  color: var(--table-head-text-color) !important;
  background: var(--button-hover-back-color) !important;
}
/*奇数行颜色*/
>>> .el-table td {
  background: var(--table-row-odd-back-color);
}
/*偶数行颜色*/
>>> .el-table--striped .el-table__body tr.el-table__row--striped td {
  background: var(--table-row-even-back-color);
}
.el-table::before {
  background: none;
}
/*设置表格行高*/
>>> .el-table th,
>>> .el-table tr {
  padding: 0;
  height: 32px;
}
>>> .el-table__body tr,
>>> .el-table__body td {
  padding: 0;
  height: 32px;
}
>>> .el-textarea__inner {
  color: var(--input-text-color);
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 400;
}
</style>